home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15575 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: news.ucdavis.edu!quad!knight
  2. From: knight@quad.cs.ucdavis.edu (James Knight)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is wrong with this loop?
  5. Date: 19 Apr 1996 22:43:02 GMT
  6. Organization: University of California, Davis
  7. Message-ID: <4l94tm$ft7@mark.ucdavis.edu>
  8. References: <4l86la$1t9@uwm.edu> <4l8apa$kv8@spanky.pls.ov.com>
  9. NNTP-Posting-Host: quad.cs.ucdavis.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Fletcher.Glenn@ov.com (glenn@ov.com) wrote:
  13. : In article 1t9@uwm.edu, mardavuy@alpha2.csd.uwm.edu (Mario David Uy) writes:
  14. : >
  15. : >   scanf("%c", &cd);
  16. : >   while (cd != 'm' || cd ! 'f' || cd != 'o')
  17. : >    {
  18. : >    printf("Re-enter m, f, or o.\n");
  19. : >    scanf(%c", &cd);
  20. : >    }
  21. : >   ...
  22. : >}
  23. : >
  24.  
  25. : My bet is that you have forgotten that the '\n' in your input is
  26. : also a character.  Try changing your while statement from:
  27.  
  28. :    while (cd != 'm' || cd ! 'f' || cd != 'o')
  29.  
  30. : to:  while(cd != 'm' || cd != 'f' || cd != 'o' || cd != '\n')
  31.  
  32. Actually, that won't work, because the loop should be 
  33.  
  34.      while (ch != 'm' && cd != 'f' && cd != 'o')
  35.  
  36. The original version will match every character.  And adding the
  37. "cd != '\n'" clause to that won't help.
  38.  
  39. The scanf solution posted in the other replies is what is
  40. required (adding a space after the %c).
  41.  
  42. Jim
  43.  
  44.